/*
 * Created on Feb 22, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.OpenPurchaseOrderForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.sales.dao.SalesDAO;

/**
 * @author Ravikanth
**/
public class OpenPurchaseOrderUpdateCommand extends ActionCommand
{
	/* 
	 * Action used to load the Open Purchase Order 
	 */
	public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)	throws Exception
	{
		HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 	= "openPurchaseOrder";
		String action   		= request.getParameter( REQUEST_ACTION );
		HttpSession mSession 	= request.getSession();
		OpenPurchaseOrderForm purchaseOrderForm = ( OpenPurchaseOrderForm ) form;
        String path = request.getParameter( "path");
        path = (path == null ? "": path.trim());
		try
		{
			if( ACTION_UPDATE.equalsIgnoreCase( action ) )
			{
				PurchaseOrderDAO.loadOpenPurchaseOrder( purchaseOrderForm );
				purchaseOrderForm.setShippingMethodsList( NewCustomerDAO.loadAllShippingMethods ( null, -1 ) );
                purchaseOrderForm.setPayTermsList( SalesDAO.getAllShippingTerms() );
/*
				purchaseOrderForm.setBillingAddress( PurchaseOrderDAO.getClientAddress() );
				purchaseOrderForm.setShippingAddress( CustomerDAO.getAddress( purchaseOrderForm.getShippingAddress_id() ) );
*/
                // start : added by murali krishna on 29/3/05 to display message that the purchase order is cancelled and needs approval
                if( "searchPendingPurchaseOrder".equals( path))
                {
                    if ( "New".equalsIgnoreCase(purchaseOrderForm.getPoStatus()) && purchaseOrderForm.isCancel() )
                    {
                        errors.put( purchaseOrderForm.getPoNumber(),"This Purchase Order has been cancelled and needs approval");
                    }
                }
                // end
                
				request.setAttribute ( "financeReasonCodes", PurchaseOrderDAO.loadFinanceHolds() );
				request.setAttribute ( "opsReasonCodes", PurchaseOrderDAO.loadOperationsHolds() );
				actionForward = "openPurchaseOrder";
			}
		}
		catch ( Exception e )
		{
			Logger.error( ERROR_LOADING_OPEN_PURCHASE_ORDER_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, "" );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "saveSuccess";
		}
		        
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}
}
